<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 新增/Add -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <link rel="stylesheet" href="../../css/diy.css">
    <script src="../../js/axios.min.js"></script>

    <style>
        img {
            width: 200px;
        }


    </style>
</head>

<body>
<article class="sign_in">
    <div class="warp tpl">
        <div class="layui-container">
            <div class="layui-row">
                <form class="layui-form" action="">
                    <div class="from-input-box-i">
                        <div class="layui-form-item unique" id="recruitment_company_box">
                            <label class="layui-form-label">招聘公司</label>
                            <div class="layui-input-block input-i block">
                                <input type="text" name="title" lay-verify="title" autocomplete="off"
                                       placeholder="请输入招聘公司"
                                       class="layui-input" id="recruitment_company">
                            </div>
                        </div>
                        <div class="layui-form-item unique" id="recruitment_positions_box">
                            <label class="layui-form-label">招聘职位</label>
                            <div class="layui-input-block input-i block">
                                <input type="text" name="title" lay-verify="title" autocomplete="off"
                                       placeholder="请输入招聘职位"
                                       class="layui-input" id="recruitment_positions">
                            </div>
                        </div>
                        <div class="layui-upload " id="describe_photos_box">
                            <button type="button" class="layui-btn" id="describe_photos">上传图片</button>
                            <div class="layui-upload-list">
                                <img class="layui-upload-img" id="describe_photos_img">
                                <p id="demoText"></p>
                            </div>
                            <div style="width: 95px;">
                                <div class="layui-progress layui-progress-big" lay-showpercent="yes"
                                     lay-filter="describe_photos">
                                    <div class="layui-progress-bar" lay-percent=""></div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item" id="contact_information_box">
                            <label class="layui-form-label">联系方式</label>
                            <div class="layui-input-block input-i block">
                                <input type="text" name="title" lay-verify="phone_number" autocomplete="off"
                                       placeholder="请输入联系方式"
                                       class="layui-input" id="contact_information">
                            </div>
                        </div>
                        <div class="layui-form-item unique" id="contact_address_box">
                            <label class="layui-form-label">联系地址</label>
                            <div class="layui-input-block input-i block">
                                <input type="text" name="title" lay-verify="title" autocomplete="off"
                                       placeholder="请输入联系地址"
                                       class="layui-input" id="contact_address">
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text" id="recruitment_requirements_box">
                            <label class="layui-form-label">招聘要求</label>
                            <div class="layui-input-block text">
                                            <textarea placeholder="请输入招聘要求" class="layui-textarea"
                                                      id="recruitment_requirements"></textarea>
                            </div>
                        </div>


                    </div>
                </form>
                <div class="layui-btn-container">
                    <button type="button" class="layui-btn layui-btn-normal login" id="submit">确认</button>
                    <button type="button" class="layui-btn layui-btn-normal login" id="cancel">取消</button>
                </div>
            </div>
        </div>
    </div>
</article>
</body>
<script src="../../layui/layui.js"></script>
<script src="../../js/base.js"></script>
<script src="../../js/index.js"></script>

<script>
    var BaseUrl = baseUrl()
    let cancel = document.querySelector("#cancel")
    cancel.addEventListener("click", () => {
        colseLayer()
    })
    let recruitment_information_id = location.search.substring(1)
    layui.use(['upload', 'element', 'layer', 'laydate', 'layedit'], function () {
        var $ = layui.jquery
            , upload = layui.upload
            , element = layui.element
            , layer = layui.layer
            , laydate = layui.laydate
            , layedit = layui.layedit
            , form = layui.form;

        let url

        let token = sessionStorage.token || null
        let personInfo = JSON.parse(sessionStorage.personInfo)
        let user_group = personInfo.user_group
        let use_id = personInfo.user_id


        function $get_stamp() {
            return new Date().getTime();
        }

        function $get_rand(len) {
            var rand = Math.random();
            return Math.ceil(rand * 10 ** len);
        }


        // 权限判断
        /**
         * 获取路径对应操作权限 鉴权
         * @param {String} action 操作名
         */
        function $check_action(path1, action = "get") {
            var o = $get_power(path1);
            if (o && o[action] != 0 && o[action] != false) {
                return true;
            }
            return false;
        }

        /**
         * 是否有显示或操作字段的权限
         * @param {String} action 操作名
         * @param {String} field 查询的字段
         */
        function $check_field(action, field, path1) {
            var o = $get_power(path1);
            var auth;
            if (o && o[action] != 0 && o[action] != false) {
                auth = o["field_" + action];
            }
            if (auth) {
                return auth.indexOf(field) !== -1;
            }
            return false;
        }

        /**
         * 获取权限
         * @param {String} path 路由路径
         */
        function $get_power(path) {
            var list_data = JSON.parse(sessionStorage.list_data)
            var list = list_data;
            var obj;
            for (var i = 0; i < list.length; i++) {
                var o = list[i];
                if (o.path === path) {
                    obj = o;
                    break;
                }
            }
            return obj;
        }

        let submit = document.querySelector('#submit')
        // 提交按钮校验权限
        if (user_group == "管理员" || $check_action('/recruitment_information/view', 'add') || $check_action('/recruitment_information/view', 'set') || $check_option('/recruitment_information/table', 'examine')) {
        } else {
            $("#submit").hide()
        }
        // style="display: none"


        let field = "recruitment_information_id";
        let url_add = "recruitment_information";
        let url_set = "recruitment_information";
        let url_get_obj = "recruitment_information";
        let url_upload = "recruitment_information"
        let query = {
            "recruitment_information_id": 0,
        }

        let form_data2 = {
            "recruitment_company": '', // 招聘公司
            "recruitment_positions": '', // 招聘职位
            "describe_photos": '', // 描述照片
            "contact_information": '', // 联系方式
            "contact_address": '', // 联系地址
            "recruitment_requirements": '', // 招聘要求
            "recruitment_information_id": 0, // ID
        }

        layui.layedit.set({
            uploadImage: {
                url: BaseUrl + '/api/recruitment_information/upload?' //接口url
                , type: 'post' //默认post
            }
        });


        var path1

        function getpath() {
            var list_data = JSON.parse(sessionStorage.list_data)
            for (var i = 0; i < list_data.length; i++) {
                var o = list_data[i];
                if (o.path === "/recruitment_information/table") {
                    path1 = o.path
                    $get_power(o.path)
                }
            }
        }

        getpath()

        /**
         * 注册时是否有显示或操作字段的权限
         * @param {String} action 操作名
         * @param {String} field 查询的字段
         * @param {String} path 路径
         */
        function $check_register_field(action, field, path1) {
            var o = $get_power(path1);
            var auth;
            if (o && o[action] != 0 && o[action] != false) {
                auth = o["field_" + action];
            }
            if (auth) {
                return auth.indexOf(field) !== -1;
            }
            return false;
        }

        /**
         * 是否有显示或操作字段的权限
         * @param {String} action 操作名
         * @param {String} field 查询的字段
         */
        function $check_field(action, field) {
            var o = $get_power("/recruitment_information/view");
            var auth;
            if (o && o[action] != 0 && o[action] != false) {
                auth = o["field_" + action];
            }
            if (auth) {
                return auth.indexOf(field) !== -1;
            }
            return false;
        }

        /**
         * 获取路径对应操作权限 鉴权
         * @param {String} action 操作名
         */
        function $check_exam(path1, action = "get") {
            var o = $get_power(path1);
            if (o) {
                var option = JSON.parse(o.option);
                if (option[action])
                    return true
            }
            return false;
        }

        function $check_option(path, op) {
            var o = $get_power(path);
            if (o) {
                var option = JSON.parse(o.option);
                if (option[op])
                    return true
            }
            return false;
        }

        /**
         * 是否有审核字段的权限
         */
        function $check_examine() {
            var url = window.location.href;
            var url_ = url.split("/")
            var pg_url = url_[url_.length - 2]
            let path = "/" + pg_url + "/table"
            var o = $get_power(path);
            if (o) {
                var option = JSON.parse(o.option);
                if (option.examine)
                    return true
            }
            return false;
        }

        if (user_group === '管理员') {
            $("#recruitment_company_box").show()
        } else {
            if ($check_field('add', 'recruitment_company')) {
                $("#recruitment_company_box").show()
            } else {
                $("#recruitment_company_box").hide()
            }
        }
        if (user_group === '管理员') {
            $("#recruitment_positions_box").show()
        } else {
            if ($check_field('add', 'recruitment_positions')) {
                $("#recruitment_positions_box").show()
            } else {
                $("#recruitment_positions_box").hide()
            }
        }
        if (user_group === '管理员') {
            $("#describe_photos_box").show()
        } else {
            if ($check_field('add', 'describe_photos')) {
                $("#describe_photos_box").show()
            } else {
                $("#describe_photos_box").hide()
            }
        }
        if (user_group === '管理员') {
            $("#contact_information_box").show()
        } else {
            if ($check_field('add', 'contact_information')) {
                $("#contact_information_box").show()
            } else {
                $("#contact_information_box").hide()
            }
        }
        if (user_group === '管理员') {
            $("#contact_address_box").show()
        } else {
            if ($check_field('add', 'contact_address')) {
                $("#contact_address_box").show()
            } else {
                $("#contact_address_box").hide()
            }
        }
        if (user_group === '管理员') {
            $("#recruitment_requirements_box").show()
        } else {
            if ($check_field('add', 'recruitment_requirements')) {
                $("#recruitment_requirements_box").show()
            } else {
                $("#recruitment_requirements_box").hide()
            }
        }

        //常规使用 - 普通图片上传
        var uploadInst = upload.render({
            elem: '#describe_photos'
            , url: BaseUrl + '/api/recruitment_information/upload?' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            , headers: {
                'x-auth-token': token
            }, before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#describe_photos_img').attr('src', fullUrl(BaseUrl, result)); //图片链接（base64）
                });

                element.progress('describe_photos', '0%'); //进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功的一些操作
                //……
                form_data2.describe_photos = res.result.url
                $('#demoText').html(''); //置空上传失败的状态
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
            //进度条
            , progress: function (n, elem, e) {
                element.progress('describe_photos', n + '%'); //可配合 layui 进度条元素使用
                if (n == 100) {
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });


        //文本
        let recruitment_company = document.querySelector("#recruitment_company")
        recruitment_company.onkeyup = function (event) {
            form_data2.recruitment_company = event.target.value
        }
        //文本
        //文本
        let recruitment_positions = document.querySelector("#recruitment_positions")
        recruitment_positions.onkeyup = function (event) {
            form_data2.recruitment_positions = event.target.value
        }
        //文本
        //文本
        let contact_address = document.querySelector("#contact_address")
        contact_address.onkeyup = function (event) {
            form_data2.contact_address = event.target.value
        }
        //文本
        //多文本
        let recruitment_requirements = document.querySelector("#recruitment_requirements")
        //多文本
        var data = sessionStorage.data || ''
        if (data !== '') {
            var data2 = JSON.parse(data)
            Object.keys(form_data2).forEach(key => {
                Object.keys(data2).forEach(dbKey => {
                    if (key === dbKey) {
                        if (key !== 'examine_state' && key !== 'examine_reply') {
                            $('#' + key).val(data2[key])
                            form_data2[key] = data2[key]
                            $('#' + key).attr('disabled', 'disabled')


                        }
                    }
                })
            })
            sessionStorage.removeItem("data");
        }


        if (recruitment_information_id !== '') {
            async function axios_get_3() {
                const {data: rese} = await axios.get(BaseUrl + '/api/recruitment_information/get_obj', {
                    params: {
                        recruitment_information_id: recruitment_information_id
                    }, headers: {
                        'x-auth-token': token
                    }
                })

                let data = rese.result.obj
                Object.keys(form_data2).forEach((key) => {
                    form_data2[key] = data[key];
                    $("#" + key).val(form_data2[key])
                });


                if (user_group === '管理员') {
                    $("#recruitment_company_box").show()
                } else {
                    if ($check_field('set', 'recruitment_company') || $check_field('get', 'recruitment_company')) {
                        $("#recruitment_company_box").show()
                    } else {
                        $("#recruitment_company_box").hide()
                    }
                }
                if (user_group === '管理员') {
                    $("#recruitment_positions_box").show()
                } else {
                    if ($check_field('set', 'recruitment_positions') || $check_field('get', 'recruitment_positions')) {
                        $("#recruitment_positions_box").show()
                    } else {
                        $("#recruitment_positions_box").hide()
                    }
                }
                if (user_group === '管理员') {
                    $("#describe_photos_box").show()
                } else {
                    if ($check_field('set', 'describe_photos') || $check_field('get', 'describe_photos')) {
                        $("#describe_photos_box").show()
                    } else {
                        $("#describe_photos_box").hide()
                    }
                }
                if (user_group === '管理员') {
                    $("#contact_information_box").show()
                } else {
                    if ($check_field('set', 'contact_information') || $check_field('get', 'contact_information')) {
                        $("#contact_information_box").show()
                    } else {
                        $("#contact_information_box").hide()
                    }
                }
                if (user_group === '管理员') {
                    $("#contact_address_box").show()
                } else {
                    if ($check_field('set', 'contact_address') || $check_field('get', 'contact_address')) {
                        $("#contact_address_box").show()
                    } else {
                        $("#contact_address_box").hide()
                    }
                }
                if (user_group === '管理员') {
                    $("#recruitment_requirements_box").show()
                } else {
                    if ($check_field('set', 'recruitment_requirements') || $check_field('get', 'recruitment_requirements')) {
                        $("#recruitment_requirements_box").show()
                    } else {
                        $("#recruitment_requirements_box").hide()
                    }
                }

                // Array.prototype.slice.call(document.getElementsByTagName('input')).map(i => i.disabled = true)
                // Array.prototype.slice.call(document.getElementsByTagName('select')).map(i => i.disabled = true)
                // Array.prototype.slice.call(document.getElementsByTagName('textarea')).map(i => i.disabled = true)
                //文本
                recruitment_company.value = form_data2.recruitment_company
                //文本


                if (user_group === '管理员' || (form_data2['recruitment_information_id'] && $check_field('set', 'recruitment_company')) || (!form_data2['recruitment_information_id'] && $check_field('add', 'recruitment_company'))) {
                } else {
                    $("#recruitment_company").attr("disabled", true);
                    $("#recruitment_company > input[name='file']").attr('disabled', true);
                }
                //文本
                recruitment_positions.value = form_data2.recruitment_positions
                //文本


                if (user_group === '管理员' || (form_data2['recruitment_information_id'] && $check_field('set', 'recruitment_positions')) || (!form_data2['recruitment_information_id'] && $check_field('add', 'recruitment_positions'))) {
                } else {
                    $("#recruitment_positions").attr("disabled", true);
                    $("#recruitment_positions > input[name='file']").attr('disabled', true);
                }


                if (user_group === '管理员' || (form_data2['recruitment_information_id'] && $check_field('set', 'describe_photos')) || (!form_data2['recruitment_information_id'] && $check_field('add', 'describe_photos'))) {
                } else {
                    $("#describe_photos").attr("disabled", true);
                    $("#describe_photos > input[name='file']").attr('disabled', true);
                }


                if (user_group === '管理员' || (form_data2['recruitment_information_id'] && $check_field('set', 'contact_information')) || (!form_data2['recruitment_information_id'] && $check_field('add', 'contact_information'))) {
                } else {
                    $("#contact_information").attr("disabled", true);
                    $("#contact_information > input[name='file']").attr('disabled', true);
                }
                //文本
                contact_address.value = form_data2.contact_address
                //文本


                if (user_group === '管理员' || (form_data2['recruitment_information_id'] && $check_field('set', 'contact_address')) || (!form_data2['recruitment_information_id'] && $check_field('add', 'contact_address'))) {
                } else {
                    $("#contact_address").attr("disabled", true);
                    $("#contact_address > input[name='file']").attr('disabled', true);
                }
                //多文本
                recruitment_requirements.value = form_data2.recruitment_requirements
                //多文本


                if (user_group === '管理员' || (form_data2['recruitment_information_id'] && $check_field('set', 'recruitment_requirements')) || (!form_data2['recruitment_information_id'] && $check_field('add', 'recruitment_requirements'))) {
                } else {
                    $("#recruitment_requirements").attr("disabled", true);
                    $("#recruitment_requirements > input[name='file']").attr('disabled', true);
                }
                let describe_photos_img = document.querySelector("#describe_photos_img")
                describe_photos_img.src = fullUrl(BaseUrl, form_data2.describe_photos)
                layui.form.render("select");
            }

            axios_get_3()
        }


        submit.onclick = async function () {
            try {
                //文本
                form_data2.recruitment_company = recruitment_company.value
                //文本
                //文本
                form_data2.recruitment_positions = recruitment_positions.value
                //文本
                //文本
                form_data2.contact_information = contact_information.value
                //文本
                //文本
                form_data2.contact_address = contact_address.value
                //文本
                //多文本
                form_data2.recruitment_requirements = recruitment_requirements.value
                //多文本
            } catch (err) {
                console.log(err)
            }
            let contact_information_phone_regular = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
            if (form_data2.contact_information && !contact_information_phone_regular.test(form_data2.contact_information)) {
                layer.msg('手机号格式错误');
                return;
            }


            let customize_field = []
            customize_field.push({"field_name": "招聘公司", "field_value": form_data2.recruitment_company});
            customize_field.push({"field_name": "招聘职位", "field_value": form_data2.recruitment_positions});
            customize_field.push({
                "field_name": "描述照片",
                "field_value": form_data2.describe_photos,
                "type": "image"
            });
            customize_field.push({"field_name": "联系方式", "field_value": form_data2.contact_information});
            customize_field.push({"field_name": "联系地址", "field_value": form_data2.contact_address});
            customize_field.push({"field_name": "招聘要求", "field_value": form_data2.recruitment_requirements});


            if (recruitment_information_id == '') {
                console.log("新增/Add")
                const {data: res} = await axios.post(BaseUrl + '/api/recruitment_information/add?',
                    form_data2, {
                        headers: {
                            'x-auth-token': token,
                            'Content-Type': 'application/json'
                        }
                    })
                if (res.result == 1) {
                    layer.msg('确认完毕');
                    setTimeout(function () {
                        colseLayer()
                    }, 1000)
                } else {
                    layer.msg(res.error.message);
                }
            } else {
                console.log("详情/Details")

                const {data: res} = await axios.post(BaseUrl + '/api/recruitment_information/set?recruitment_information_id=' + recruitment_information_id,
                    form_data2, {
                        headers: {
                            'x-auth-token': token,
                            'Content-Type': 'application/json'
                        }
                    })
                if (res.result == 1) {
                    layer.msg('确认完毕');
                    setTimeout(function () {
                        colseLayer()
                    }, 1000)
                } else {
                    layer.msg(res.error.message);
                }
            }
        }

    })
    ;
</script>

</html>
